package cn.niit.letuseatdemo.util;


import cn.niit.letuseatdemo.common.Constants;
import cn.niit.letuseatdemo.entity.domain.SysUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



/**
* @Description: 获取HttpSession、当前登录用户对象  SysUser  的工具类
* @Author: Zhang Shiqi
* @CreateTime： 2022/10/31 1:27
**/
public class LoginUserUtil {

    /*
      * @Description: 通过 RequestContextHolder 获取当前用户的 HttpSession 对象
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/3  15:42
 * @return: javax.servlet.http.HttpSession
      **/
    public  static HttpSession getHttpSession(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }

/*
* @Description:获取当前登录用户SysUser对象
* @Author: Zhang Shiqi
* @CreateTime： 2022/10/31 1:27
 * @return: cn.niit.letuseatdemo.entity.domain.SysUser
**/
 public static SysUser getLoginSysUser(){
        Object obj = getHttpSession().getAttribute(Constants.ADMIN_SESSION_USER_KEY);
        if (null != obj){
            return (SysUser) obj;

        }
        return null;
    }
}
